/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ntb.common;

/**
 * Render pagination
 * 
 * @author Thinh Hung
 */
public class PaginationUtil {

    private String baseUrl = ""; // The page we are linking to
    private String prefix = ""; // A custom prefix added to the path.
    private String suffix = ""; // A custom suffix added to the path.
    private int numPages = 1; // Total number of pages
    private int numLinks = 3; // Number of "digit" links to show before/after the currently viewed page
    private int curPage = 0; // The current page being viewed
    private String prevLink = "Prev";
    private String nextLink = "Next";
    private boolean queryString = true;
    private String queryStringSegment = "page";

    public PaginationUtil() {
    }

    public String createLinks() {
        if (this.numPages <= 1) {
            return "";
        }

        if (this.curPage < 1) {
            this.curPage = 1;
        }

        if (this.curPage > this.numPages) {
            this.curPage = this.numPages;
        }

        if (this.numLinks < 1) {
            this.numLinks = 1;
        }

        // Calculate the start and end numbers. These determine
        // which number to start and end the digit links with
        int start = this.curPage - this.numLinks;
        int end = this.curPage + this.numLinks;

        if ((this.curPage - this.numLinks - 1) <= 1) {
            start = 1;
            if ((this.numLinks * 2 + 1) > (this.curPage + this.numLinks)) {
                end = this.numLinks * 2 + 1;
            }
        }
        if ((this.curPage + this.numLinks + 1) >= this.numPages) {
            end = this.numPages;
            if ((this.numPages - (this.numLinks * 2)) < (this.curPage - this.numLinks)) {
                start = this.numPages - (this.numLinks * 2);
            }
        }

        start = start < 1 ? 1 : start;
        end = end > this.numPages ? numPages : end;

        // Is pagination being used over GET or POST?  If get, add a per_page query
        // string. If post, add a trailing slash to the base URL if needed
        if (this.queryString) {
            this.baseUrl = this.baseUrl.trim() + "&amp;" + queryStringSegment + "=";
        } else {
            this.baseUrl = this.baseUrl + "/";
        }

        // And here we go...
        String output = "";

        // Render the "previous" link
        if (this.curPage != 1) {
            int i = this.curPage - 1;

            String n = (i <= 1) ? i + "" : this.prefix + i + this.suffix;

            output += "<a href=\"" + this.baseUrl + n + "\">" + this.prevLink + "</a>";
        }

        // Render the "First" link
        if (this.curPage > (this.numLinks + 1 + 1)) {
            output += "<a href=\"" + this.baseUrl + "\">1</a><span class=\"gap\">&hellip;</span>";
        }

        // Render the pages
        for (int i = start; i <= end; i++) {
            // Write the digit links
            if (i >= 1) {
                if (this.curPage == i) {
                    output += "<strong>" + i + "</strong>";
                } else {
                    String n = (i <= 1) ? i + "" : this.prefix + i + this.suffix;
                    output += "<a href=\"" + this.baseUrl + n + "\">" + i + "</a>";
                }
            }
        }

        // Render the "Last" link
        if (this.curPage < (this.numPages - this.numLinks - 1)) {
            output += "<span class=\"gap\">&hellip;</span><a href=\"" + this.baseUrl + this.prefix + numPages + this.suffix + "\">" + this.numPages + "</a>";
        }

        // Render the "next" link
        if (this.curPage < this.numPages) {
            int i = this.curPage + 1;

            output += "<a href=\"" + this.baseUrl + this.prefix + i + this.suffix + "\">" + this.nextLink + "</a>";
        }

        // Kill double slashes.  Note: Sometimes we can end up with a double slash
        // in the penultimate link so we'll kill all double slashes.
        output.replaceAll("#([^:])//+#", "\\1/");

        return output;
    }

    public String getBaseUrl() {
        return baseUrl;
    }

    public void setBaseUrl(String baseUrl) {
        this.baseUrl = baseUrl;
    }

    public int getCurPage() {
        return curPage;
    }

    public void setCurPage(int curPage) {
        this.curPage = curPage;
    }

    public String getNextLink() {
        return nextLink;
    }

    public void setNextLink(String nextLink) {
        this.nextLink = nextLink;
    }

    public int getNumLinks() {
        return numLinks;
    }

    public void setNumLinks(int numLinks) {
        this.numLinks = numLinks;
    }

    public int getNumPages() {
        return numPages;
    }

    public void setNumPages(int numPages) {
        this.numPages = numPages;
    }

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public String getPrevLink() {
        return prevLink;
    }

    public void setPrevLink(String prevLink) {
        this.prevLink = prevLink;
    }

    public boolean isQueryString() {
        return queryString;
    }

    public void setQueryString(boolean queryString) {
        this.queryString = queryString;
    }

    public String getQueryStringSegment() {
        return queryStringSegment;
    }

    public void setQueryStringSegment(String queryStringSegment) {
        this.queryStringSegment = queryStringSegment;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }
}
